<template>
	<div class="component-box">
		<el-select class="w-ipt" v-model="c_aip_id" placeholder="请选择百度接口" @change="baiduAipChange" filterable>
			<el-option v-for="(item,index) in baidu_aip_list" :key="index" :label="item.name" :value="item.id">
			</el-option>
			<div v-if="loading_more" class="more">
				<el-button type="primary" size="mini" @click="limitChange">加载更多</el-button>
			</div>
			<div v-else class="nomore">
				<el-button type="text" size="mini" disabled>暂无更多数据</el-button>
			</div>
		</el-select>
	</div>
</template>

<script>
	import API from '@/api/index.js'

	export default {
		data() {
			return {
				c_aip_id: this.aip_id,
				baidu_aip_list: [],
				loading_more: true,
				page: 1,
				limit: '10'
			}
		},
		props: ['aip_id'],
		watch: {
			aip_id(val) {
				this.c_aip_id = val;
			}
		},
		mounted() {
			this.getBaiduAip();
		},
		methods: {
			// 获取百度接口
			getBaiduAip() {
				let self = this;
				API.aips(1, 10).then(res => {
					let data = res.data;
					self.$notify({
						message: '获取百度数据成功'
					});
					self.baidu_aip_list = data.data;
					self.page = self.page + 1;
				}).catch(err => {
					self.$message.error(err.response.data.message)
				})
			},
			// 加载更多数据
			limitChange() {
				let self = this;
				API.aips(self.page, self.limit).then(res => {
					let data = res.data;
					self.$notify({
						message: '加载百度数据成功'
					});
					if (data.data.length > 0) {
						self.baidu_aip_list = self.baidu_aip_list.concat(data.data);
						self.page = self.page + 1;
					} else {
						self.loading_more = false;
					}
				}).catch(err => {
					self.$message.error(err.response.data.message)
				});
			},

			// 选择了百度id
			baiduAipChange(val) {
				let self = this;
				self.$emit('getBaiduAipId', {
					child_aip_id: val
				});
			},
		}
	}
</script>

<style lang="scss" scoped>
	
</style>
